package relation;

import com.alibaba.fastjson.annotation.JSONField;
import my_exception.checkException;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Set;

/**
 * 字段
 * @author cgl-22341129
 */
public class Field {
    /**
     * 复制一个field类
     * @param field
     */
    public Field(Field field){
        this.fieldname = field.getFieldname();
        this.dataform = field.getDataform();
        this.key = field.getKey();
        this.defaultvalue = field.getDefaultvalue();
        this.ForeignFieldName=field.getForeignFieldName();
        this.ForeignTable=getForeignTable();
    }
//    public Field(String fieldname, String dataform, String key, Object defaultvalue) {
//        this.fieldname = fieldname;
//        this.dataform = dataform;
//        this.key = key;
//        this.defaultvalue = defaultvalue;
//        this.ForeignRelation=null;
//    }


    public Field(String fieldname, String dataform, String key, String foreignTable, String foreignFieldName) {
        this.fieldname = fieldname;
        this.dataform = dataform;
        this.key = key;
        this.defaultvalue = "null";
        ForeignTable = foreignTable;
        ForeignFieldName = foreignFieldName;
    }

    private String fieldname;
    private String dataform;
    private String key;
    private Object defaultvalue;
    /**
     * 字段的外键依赖关系
     */
    private String ForeignTable;

    public String getForeignFieldName() {
        return ForeignFieldName;
    }

    public String getForeignTable() {
        return ForeignTable;
    }

    private String ForeignFieldName;

    public String getFieldname() {
        return fieldname;
    }

    public void setFieldname(String fieldname) {
        this.fieldname = fieldname;
    }

    public String getDataform() {
        return dataform;
    }

    public void setDataform(String dataform) {
        this.dataform = dataform;
    }
    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
    public void setDefaultvalue(String defaultvalue) {
        this.defaultvalue = defaultvalue;
    }

    public Object getDefaultvalue() {
        return defaultvalue;
    }

    public void showfield(){

        if(Objects.equals(this.key, "FK"))
            System.out.println(fieldname+"  "+dataform+"  "+key+"  "+defaultvalue+"  ("+this.ForeignTable+"  "+this.ForeignFieldName+")");
        else
            System.out.println(fieldname+"  "+dataform+"  "+key+"  "+defaultvalue+"  null");
    }

    /**
     * 判断ArrayList中是否含有名字为fieldname的属性
     * @param fields 表结构的所有字段的数组
     * @param fieldname 需要查找的字段名
     * @return 是否含有这个字段,是为true,否为false
     */
    static public boolean contains(ArrayList<Field> fields,String fieldname){
        boolean flag=false;
        for(Field field:fields){
            if (fieldname.equals(field.getFieldname())) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    static void newFieldcheck(Field field) throws checkException {
        if("FK".equals(field.getKey())&&(field.getForeignTable()==null||field.getForeignFieldName()==null))
            throw new checkException("属性具有外键约束，但缺少依赖的信息");
    }

}
